package com.products.gui;

//import java.awt.BorderLayout;
//import com.products.utils.TimeThinker;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.*;
import com.products.dao.Commands;
import com.products.model.Product;
import com.products.utils.ProductComboBox;



public class Frame extends JApplet {
	private static final long serialVersionUID = 1L;
	private JPanel panel = new JPanel();
	private ProductComboBox combo = new ProductComboBox();
	private JTextArea textArea = new JTextArea(6, 16);
	private JButton button1 = new JButton("Заказать");
	private Commands commands = new Commands();
	private List<Product> products = null;
	private JTextField textField1 = new JTextField();
	
    @Override
	public void init() {
//		initTimer();
		initComponents();
		products = commands.getList();
		fillUI(products);

		button1.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent arg0) {
				int amout = 1;
				try{
					amout = Integer.parseInt(textField1.getText());
				}
				catch (Exception e) {
					amout = 1;
				}
				commands.addProduct(combo.getProduct(), amout);
				products = commands.getList();
				fillUI(products);
			}
		});
	}

	private void initComponents() {
		panel = new JPanel();
		GridBagConstraints gbc;

		setLayout(new BorderLayout());

		{
			panel.setBorder(new TitledBorder("Products order"));
			panel.setLayout(new GridBagLayout());
			((GridBagLayout)panel.getLayout()).columnWidths = new int[] {205, 100};
			gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.insets.bottom = 5;
			gbc.insets.right = 5;
			panel.add(combo, gbc);
			
			gbc = new GridBagConstraints();
			gbc.gridx = 1;
			gbc.gridy = 0;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.insets.bottom = 5;
			panel.add(textField1, gbc);
			
			gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = 1;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.insets.bottom = 5;
			gbc.insets.right = 5;
			gbc.gridwidth = 5;
			panel.add(textArea, gbc);
			
			gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.gridy = 2;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.insets.right = 5;
			panel.add(button1, gbc);
		}
		add(panel, BorderLayout.CENTER);
		setSize(400,300);
	}
	
	private void fillUI(List<Product> products) {
		textArea.setText("");
		combo.setItems(products);
		for (int i = 0; i < products.size(); i++) {
			Product product = products.get(i);
			textArea.append(product.toString()+ "\n");
		}
	}
	
//	private void initTimer(){
//		TimeThinker thinker = new TimeThinker(this);
//		Thread thread = new Thread(thinker);
//		thread.start();
//	}
	
	public List<Product> getProducts(){
		return commands.getList();
	}
	
	public Commands getCommands(){
		return commands;
	}
}
